<?php
/**
 * 
 * 删除用户组
 * @author LeoNing
 *
 */
class DelGroup extends Action {
	public function execute($params) {
		$me = check_login ( ADMIN_LOGIN_PAGE );
		if (! icando ( 'delgroup', 'user' )) {
			Response::errorPage ( "你无权删除用户组.", $this->referer );
		}
		if (rqset ( 'gids' )) {
			$gid = safe_ids ( rqst ( 'gids' ), ',', true );
			$where ['gid IN'] = $gid;
		} else {
			$gid = irqst ( 'gid' );
			$where ['gid'] = $gid;
		}
		$where ['reserved'] = 0; // 只能删除非保留的用户组
		$groupModel = new CoreGroupModel ();
		$groupModel->begin ();
		$rst = apply_filter ( 'on_delete_group', true, $gid );
		if ($rst) {
			$rst = $groupModel->delete ( $where );
			if ($rst !== false && $rst > 0) {
				$guM = new CoreGroupUserModel ();
				$rst = $guM->delete ( array ('gid IN' => is_array ( $gid ) ? $gid : array ($gid ) ) ); // 删除组与用户的映射
				if ($rst !== false) {
					$groupModel->commit ();
				} else {
					$groupModel->rollback ();
				}
			} else {
				$groupModel->rollback ();
			}
		}
		if ($rst === false) {
			Response::errorPage ( "删除用户组失败。具体原因如下：<br/>" . $groupModel->last_error_msg (), $this->referer );
		} else {
			Response::redirect ( $this->referer );
		}
	}
}